﻿class RythmController extends MovieClip
{
	
	
	
	var timer:Number = -1;
	var timerReset = [180, 180, 210, 240, 270];
	/*
	16 / 20
	20 / 25
	24 / 30
	28 / 35
	32 / 40
	*/
	
	var cntArray = [20, 25, 30, 35, 40];	//4, 5, 6, 7, 8
	var goalArray = [10, 20, 24, 28, 32];	//첫 레벨은 쉽게
	var hungerArray = [8, 10, 12, 14, 16];	//2회 남았는데 이 이하면 주의소리
	var warningArray = [2, 3, 3, 4, 4];	//한 phase에 몇번 이상 틀리면 경고
	
	var startTimer = 60;
	
	
	var currentScore = 0;
	var badScore = 0;
	var warningCnt = 0;
	
	
	var RCArray:Array;
	
	var circleIdx = 0;
	
	var key:Boolean = true;
	
	var phase:Number = 0;	//4 over
	
	var posX = random(6)*100+150;
	var posY = random(4)*100+150;
	
	function RythmController(){
		
		//onLoad = Init;
		//onEnterFrame = onEF;
		
	}
	
	function Init(){
		timer = 0;
		circleIdx = 0;
		phase = 0;
		currentScore = 0;
		badScore = 0;
		warningCnt = 0;
		
		startTimer = 60;
		this.gotoAndStop(_root.STAGE+1);
		//0 - 4
		//1 - 5
		//2 - 6
		//3 - 7
		//4 - 8
		RCArray = new Array(_root.STAGE + 4);
		for(var i=0; i < RCArray.length; i++){
			RCArray[i] = this["RC"+i];
		}
		switch(_root.Language){
			case 0 :
			switch(_root.STAGE){
				case 0 :
				if(_root.fromPrologue == true){
					_root.fromPrologue = false;
				}else{
					_root.msgBox.showMessage("2010년 1월 1일", 60);	//GameOver이후에만
				}				
				break;
				case 1:
				_root.msgBox.showMessage("2010년 1월 4일", 60);			
				break;
				case 2:
				_root.msgBox.showMessage("2010년 1월 7일", 60);
				break;
				case 3:
				_root.msgBox.showMessage("2010년 1월 10일", 60);
				break;
				case 4:
				_root.msgBox.showMessage("그러던 어느 날", 60);
				break;
			}
			break;
			case 1 :
			switch(_root.STAGE){
				case 0:
				//여기에 인스트럭션? 다른색으로...
				_root.msgBox.showMessage("Day 1", 60);
				break;
				case 1:
				_root.msgBox.showMessage("Day 5", 60);
				break;
				case 2:
				_root.msgBox.showMessage("Day 8", 60);
				break;
				case 3:
				_root.msgBox.showMessage("Lost Counting Days", 60);
				break;
				case 4:
				_root.msgBox.showMessage("...", 60);
				break;
			}
			break;
		}
			
		trace("RCArray " + RCArray);
		
	}
	
	function onEF(){
		
		timer++;
		/*
		//좀 있다 시작하게
		if(timer != startTimer){
			timer = 0;
			startTimer = -1;
			
			return;
		}
		*/
		
		
		for(var i in _root.blinkArray[_root.STAGE][phase]){
			if(timer == _root.blinkArray[_root.STAGE][phase][i]){
				
				showRythmCircle();
				circleIdx++;
				circleIdx %= (_root.STAGE + 4);
			}
		}
		
		/*
		if(Key.isDown(32) && !key){
			
			trace("Skip Scene called");
			currentScore = 0;
			badScore = 0;
			phase = 0;
			
			for(var i =0; i< 5; i++){
				_root["BridgeScene"+i].gotoAndStop(1);
			}
			_root["BridgeScene"+_root.STAGE].gotoAndPlay(2);	//Scene timeline MC start
			trace("play : " + _root["BridgeScene"+_root.STAGE]);
			//_root.attachMovie("MC_FO", "FO", _root.getNextHighestDepth());
			delete onEnterFrame;
			
			
			//trace("presse");
		}else if(!Key.isDown(32) && key){
			
			//trace("release");
		}
		
		key = Key.isDown(32);
		*/
		
		
		
		if(warningCnt >= warningArray[_root.STAGE]){
			if(_root.Sister._visible){
				switch(random(2)){
					case 0:
					
					_root.msgBoxSister.showMessage("Oppa! Be careful!", 30);
					break;
					case 1:				
					_root.msgBoxSister.showMessage("Oppa! They might hear you.", 30);
					break;
				}
			}
			
			warningCnt = -100;
			
		}
		
		if(timer == timerReset[_root.STAGE]){
			timer = 0;
			
			warningCnt = 0;
			//warningCnt는 리셋
			
			phase++;
			
			posX = random(6)*100+150;
			posY = random(4)*100+150;
			
			if(badScore > (cntArray[_root.STAGE] - goalArray[_root.STAGE])){
				//game over					
				//_root.Door.gotoAndStop("open");
				currentScore = 0;
				badScore = 0;
				phase = 0;
				circleIdx = 0;
				delete onEnterFrame;
				_root.gotoAndStop("gameover");
			}
			
			
			
			//중반이 지났는데 수가 모자라면 헝그리 소리
			if(phase == 3){
				if(currentScore <= hungerArray[_root.STAGE]){
					_root.snd_hunger.start();
					if(_root.Sister._visible){
						_root.msgBoxSister.showMessage("Oppa... aren't you hungry...?", 60);
					}
					
				}				
			}
			
			if(phase == 5){
				trace("Score : " + currentScore);
				
				if(currentScore >= goalArray[_root.STAGE]){
					//play animation & go to next level
					currentScore = 0;
					badScore = 0;
					phase = 0;
					delete onEnterFrame;
					_root.Door.gotoAndStop("open");
					
				}else{
					//굶어죽는 Game Over 애니메이션
					currentScore = 0;
					badScore = 0;
					phase = 0;
					delete onEnterFrame;
					_root.gotoAndStop("gameoverHunger");
				}
				
				
			}
			
		}
	}
	
	
	
	
	function showRythmCircle(X, Y){
		//trace("showRC");
		
		//RCArray[circleIdx].onMouseDown = RCArray[circleIdx].playSound;
		RCArray[circleIdx].onPress = RCArray[circleIdx].playSound;
		RCArray[circleIdx].gotoAndPlay(2);
		//RCArray[circleIdx]._x = _Arr[1];
		//RCArray[circleIdx]._y = _Arr[2];
		
		RCArray[circleIdx]._x = posX;
		RCArray[circleIdx]._y = posY;
		//_root.snd_knock.start();
		
		//_Arr[1].start();
		
		
		
	}
	
}